﻿using AutoMapper;
using MediatR;
using SmartProduction.Domain.采购委外订单;
using SmartProduction.ErrorCode;
using SmartProduction.Infarstructrue.Interface;
using SmartProduction.Write.API.Application.Commands.SupplierCommand;

namespace SmartProduction.Write.API.Application.CommandHandlers.SupplierCommandHandler
{
    public class SupplierDeleteCommandHandler : IRequestHandler<SupplierDeleteCommand, APIResult<object>>
    {
        private readonly IMapper mapper;
        private readonly IBaseRepository<Supplier> supplierRepository;

        public SupplierDeleteCommandHandler(IMapper mapper, IBaseRepository<Supplier> supplierRepository)
        {
            this.mapper = mapper;
            this.supplierRepository = supplierRepository;
        }

        public async Task<APIResult<object>> Handle(SupplierDeleteCommand request, CancellationToken cancellationToken)
        {
            APIResult<object> result = new APIResult<object>();

            var entity = supplierRepository.GetAll().Result.FirstOrDefault(x => x.SupplierId == request.SupplierId);

            entity.IsDelete = 1;
            var info = supplierRepository.UpdateAsync(entity).Result;
            if (info > 0)
            {
                result.Code = APIEnums.删除成功;
                result.Msg = APIEnums.删除成功.ToString();
                result.data = info;
            }
            else
            {
                result.Code = APIEnums.删除失败;
                result.Msg = APIEnums.删除失败.ToString();
            }
            return await Task.FromResult(result);
        }
    }
}
